package com.ruoyi.teacher.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.teacher.domain.TQuestion;
import com.ruoyi.teacher.mapper.TQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.TQuestionBankMapper;
import com.ruoyi.teacher.domain.TQuestionBank;
import com.ruoyi.teacher.service.ITQuestionBankService;

/**
 * 题库Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-30
 */
@Service
public class TQuestionBankServiceImpl implements ITQuestionBankService
{
    @Autowired
    private TQuestionBankMapper tQuestionBankMapper;

    /**
     * 查询题库
     *
     * @param id 题库主键
     * @return 题库
     */
    @Override
    public TQuestionBank selectTQuestionBankById(Long id)
    {
        return tQuestionBankMapper.selectTQuestionBankById(id);
    }

    /**
     * 查询题库列表
     *
     * @param tQuestionBank 题库
     * @return 题库
     */
    @Override
    public List<TQuestionBank> selectTQuestionBankList(TQuestionBank tQuestionBank)
    {
        return tQuestionBankMapper.selectTQuestionBankList(tQuestionBank);
    }

    /**
     * 新增题库
     *
     * @param tQuestionBank 题库
     * @return 结果
     */
    @Override
    public int insertTQuestionBank(TQuestionBank tQuestionBank)
    {
        tQuestionBank.setCreateTime(DateUtils.getNowDate());
        return tQuestionBankMapper.insertTQuestionBank(tQuestionBank);
    }

    /**
     * 修改题库
     *
     * @param tQuestionBank 题库
     * @return 结果
     */
    @Override
    public int updateTQuestionBank(TQuestionBank tQuestionBank)
    {
        tQuestionBank.setUpdateTime(DateUtils.getNowDate());
        return tQuestionBankMapper.updateTQuestionBank(tQuestionBank);
    }

    /**
     * 批量删除题库
     *
     * @param ids 需要删除的题库主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionBankByIds(Long[] ids)
    {
        return tQuestionBankMapper.deleteTQuestionBankByIds(ids);
    }

    /**
     * 删除题库信息
     *
     * @param id 题库主键
     * @return 结果
     */
    @Override
    public int deleteTQuestionBankById(Long id)
    {
        return tQuestionBankMapper.deleteTQuestionBankById(id);
    }

    @Autowired
    private TQuestionMapper questionMapper;
    @Override
    public void deleteBank(TQuestionBank qbQuestionBank) {
        // 查询 t_question 表中与 b_id 关联的记录数量
        long count = questionMapper.selectCountByBid(qbQuestionBank.getId());
        if (count == 0) {
            // 如果没有关联的题目，检查 is_delete 是否为 0，如果是，恢复为 1，否则更新为 0
            int updatedRows = 0;
            if (qbQuestionBank.getIsDelete() == 0) {
                // 如果 is_delete 为 0，恢复为 1
                updatedRows = tQuestionBankMapper.updateIsDelete(qbQuestionBank.getId(), 1);
            } else {
                // 如果 is_delete 不为 0，则更新为 0
                updatedRows = tQuestionBankMapper.updateIsDelete(qbQuestionBank.getId(), 0);
            }

            if (updatedRows == 0) {
                throw new RuntimeException("更新失败，可能记录不存在");
            }
        } else {
            // 如果有关联题目，抛出异常提示
            throw new RuntimeException("题库【" + qbQuestionBank.getName() + "】还有【" + count + "】道题目存在，删除失败");
        }
    }

    @Override
    public void updateIsDelete(Long id, int i) {

    }

    @Override
    public boolean restoreBank(Long id) {
        return false;
    }


}
